package broadcastreceiver.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class BroadcastReceiverTestActivity extends Activity {
    
	private SMSReceiver receiver = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btnSendBroadcast = (Button)this.findViewById(R.id.btnSendBroadcast);
        Button btnRegisterBroadcast = (Button)this.findViewById(R.id.btnRegisterBroadcast);
        Button btnUnRegisterBroadcast = (Button)this.findViewById(R.id.btnUnRegisterBroadcast);

        //Register receiver in AndroidManifest.xml
        btnSendBroadcast.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_EDIT);
				BroadcastReceiverTestActivity.this.sendBroadcast(intent);
			}
		});
        
        //Register receiver in code
        btnRegisterBroadcast.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Log.v("SMSReceiver", "registerBroadcast");
			
				IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); 
				receiver = new SMSReceiver();
				BroadcastReceiverTestActivity.this.registerReceiver(receiver, filter);
			}
		});
        
        btnUnRegisterBroadcast.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Log.v("SMSReceiver", "unRegisterBroadcast");
				BroadcastReceiverTestActivity.this.unregisterReceiver(receiver);
			}
		});
    }
}